package org.crazyit.album.web;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;


import java.util.*;
import java.io.*;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.RequestContext;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.crazyit.album.service.AlbumService;
import org.crazyit.album.exception.AlbumException;
import org.crazyit.album.web.base.BaseServlet;

@WebServlet(urlPatterns = "/proUpload")
public class ProUploadServlet extends BaseServlet {
    public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        Iterator iter = null;
        String title = null;
        response.setContentType("text/html;charset=gbk");

        PrintWriter out = response.getWriter();
        try {

            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);
            List items = upload.parseRequest((RequestContext) request);
            iter = items.iterator();

            while (iter.hasNext()) {
                FileItem item = (FileItem) iter.next();

                if (item.isFormField()) {
                    String name = item.getFieldName();
                    if (name.equals("title")) {
                        title = item.getString("gbk");
                    }
                } else {
                    String user = (String) request.getSession().getAttribute("curUser");
                    String serverFileName = null;

                    String fileName = item.getName();

                    String suffix = fileName.substring(fileName.lastIndexOf("."));

                    String contentType = item.getContentType();

                    if (contentType.equals("image/pjpeg") || contentType.equals("image/gif") || contentType.equals("image/jpeg") || contentType.equals("image/png")) {
                        InputStream input = item.getInputStream();
                        serverFileName = UUID.randomUUID().toString();
                        FileOutputStream output = new FileOutputStream(getServletContext().getRealPath("/") + "uploadfiles\\" + serverFileName + suffix);
                        byte[] buffer = new byte[1024];
                        int len = 0;
                        while ((len = input.read(buffer)) > 0) {
                            output.write(buffer, 0, len);
                        }
                        input.close();
                        output.close();
                        as.addPhoto(user, title, serverFileName + suffix);
                        out.write("<script type='text/javascript'>" + "parent.callback(' ')" + "</script>");
                    } else {
                        out.write("<script type='text/javascript'>" + "parent.callback(' " + " ')</script>");
                    }
                }
            }
        } catch (FileUploadException fue) {
            fue.printStackTrace();
            out.write("<script type='text/javascript'>" + "parent.callback(' ')" + "</script>");
        } catch (AlbumException ex) {
            ex.printStackTrace();
        }
    }
}